﻿# -*- coding: utf-8 -*-
import time

from odoo import api, fields, models, _
import odoo.addons.decimal_precision as dp
from odoo.exceptions import UserError
import calendar
class cfarmc_project_pulic_wizard(models.TransientModel):
    _name = 'cfarmc_project.pulic_wizard'
    _description = u'向导'
    def _get_lineNO1(self):
        obj = self.env[self._context.get('modelline')]
        idsobj = obj.search([('pay_id','=',self._context.get('pay_id'))])
        default_order_line= ""
        for item in idsobj:
            if(item.fmobile1!=False):
                default_order_line=item.fmobile1
        return default_order_line

    def _get_line(self):
        obj = self.env[self._context.get('modelline')]
        idsobj = obj.search([('pay_id','=',self._context.get('pay_id'))])
        default_order_line= []
        for item in idsobj:
            default_order_line.append([0, False,{
            'fmobile1':item.fmobile1,
            'fremarks':item.fremarks,
            'FAddperson':item.FAddperson.id,
            'FAdddate':item.FAdddate,
            'Fstatus':item.Fstatus
            }])  			
        return default_order_line    
    def get_employee(self):
        obj = self.env['hr.employee']
        idsobj = obj.search([('user_id','=',self.env.user.id)])
        print('-------------------------------------')		
        print(self.env.user.id)		
        #print(ids)	
        res = idsobj.read()
        print(res)					
        return res and res[0]['id'] or 0    
    fmobile1= fields.Char(u'标题',readonly=False,required=False,translate=False,default=lambda self:self._get_lineNO1())
    fremarks=fields.Text(u'备注',readonly=False,required=False,translate=False)    
    order_line = fields.One2many('cfarmc_project.pulic_wizard.wizardline','pay_id',string='Order Lines',copy=True,default=lambda self: self._get_line())	
	

    def action_cancel(self):
        """
        Closes Phonecall to Phonecall form
        """
        return {'type':'ir.actions.act_window_close'}		     
    def ziyuanxd_fdcrm_create001(self):
        model_data = self.env['ir.model.data']
        model_self=super(cfarmc_project_pulic_wizard, self).read(['fmobile1','fremarks'])[0]
        vals={}
        vals['pay_id']=self._context.get('pay_id')       
        vals['fmobile1']=model_self['fmobile1']
        vals['fremarks']=model_self['fremarks']        
        vals['FAddperson']=self.get_employee()        
        vals['FAdddate']=fields.Datetime.now() 
        vals['state']=self._context.get('work')       
        
        if(self._context.get('work')=="application"):        
            vals['Fstatus']='-1'  
        if(self._context.get('work')=="approval1"):        
            vals['Fstatus']='0'
        if(self._context.get('work')=="approval2"):        
            vals['Fstatus']='0'
        if(self._context.get('work')=="approval3"):        
            vals['Fstatus']='0'
        if(self._context.get('work')=="approval4"):        
            vals['Fstatus']='0'
               
        result =self.env[self._context.get('modelline')].sudo().create(vals)
        tinfo=self.env[self._context.get('model')].browse(self._context.get('pay_id'))[0]
		#员工提交
        if(self._context.get('work')=="application"):
            fid=tinfo.sudo().message_post(body=_("111提交人提交"))
            fmessage=self.env['mail.message'].browse(fid.id)[0]
            notification_ids=[]
            partner_ids=[]            
            for tinfo_item in tinfo.fhgroups1.users:
                partner_ids.append(tinfo_item.partner_id.id)            
                if(self.env.user.partner_id.id != tinfo_item.partner_id.id):
                    notification_ids.append(tinfo_item.partner_id.id)                                                        
            fmessage.sudo().write({'subject':'项目审批','needaction_partner_ids':[[6, False,notification_ids]],'partner_ids':[[6, False,partner_ids]]})                        
            tinfo.sudo().action_funproved0()                

		#1级审批提交
        if(self._context.get('work')=="approval1"):
            tinfo.sudo().action_funproved1() 
            fid=tinfo.sudo().message_post(body=_("预审人员提交"))
            fmessage=self.env['mail.message'].browse(fid.id)[0]
            notification_ids=[]
            partner_ids=[]            
            for tinfo_item in tinfo.fhgroups2.users:
                partner_ids.append(tinfo_item.partner_id.id)            
                if(self.env.user.partner_id.id != tinfo_item.partner_id.id):
                    notification_ids.append(tinfo_item.partner_id.id)                                                        
            fmessage.sudo().write({'subject':'项目审批','needaction_partner_ids':[[6, False,notification_ids]],'partner_ids':[[6, False,partner_ids]]})                                           
                



		#4级审批提交
        if(self._context.get('work')=="approval4"):
            tinfo.sudo().action_funproved4()        
            fid=tinfo.sudo().message_post(body=_("终审人员提交"))
            fmessage=self.env['mail.message'].browse(fid.id)[0]

            notification_ids=[[0,False,{'mail_message_id':fid.id,'res_partner_id':tinfo.FAddperson.user_id.partner_id.id}]]             
            print('notification_ids:',notification_ids)                    
            fmessage.sudo().write({'subject':'项目审批','notification_ids':notification_ids,'partner_ids':[[6, False,[tinfo.FAddperson.user_id.partner_id.id]]]})



        return True
    def ziyuanxd_fdcrm_create002(self):
        model_data = self.env['ir.model.data']
        model_self=super(cfarmc_project_pulic_wizard, self).read(['fmobile1','fremarks'])[0]
        vals={}
        vals['pay_id']=self._context.get('pay_id')       
        vals['fmobile1']=model_self['fmobile1']
        vals['fremarks']=model_self['fremarks']
        
        vals['FAddperson']=self.get_employee()
        
        vals['FAdddate']=fields.Datetime.now()         
        vals['Fstatus']='1'	
        result =self.env[self._context.get('modelline')].sudo().create(vals)            

		#发送消息
        tinfo=self.env[self._context.get('model')].browse(self._context.get('pay_id'))[0]
		#发送给提交者	
        if(self._context.get('work')=="reject"):
            fid=tinfo.sudo().message_post(body=_("审批人拒绝"))
            fmessage=self.env['mail.message'].browse(fid.id)[0] 

           
            notification_ids=[[0,False,{'mail_message_id':fid.id,'res_partner_id':tinfo.FAddperson.user_id.partner_id.id}]]             
            print('notification_ids:',notification_ids)                    
            fmessage.sudo().write({'subject':'项目审批','notification_ids':notification_ids,'partner_ids':[[6, False,[tinfo.FAddperson.user_id.partner_id.id]]]})
	
            tinfo.sudo().action_funproved5()							
        return True



cfarmc_project_pulic_wizard()
class fhpulic_wizardline(models.TransientModel):
    _name = 'cfarmc_project.pulic_wizard.wizardline'
    _description = u'详细信息'
    def get_employee(self, cr, uid, context={}):
        obj = self.pool.get('hr.employee')
        ids = obj.search(cr, uid, [('user_id','=',uid)])
        res = obj.read(cr, uid, ids, ['id','name'], context)
        return res and res[0]['id'] or 0

    pay_id=fields.Many2one('cfarmc_project.pulic_wizard',u'fhtest_weishui001tb',required=True, ondelete='cascade', index=True, copy=False)
    fmobile1= fields.Char(u'标题',readonly=False,required=False,translate=False)
    fremarks=fields.Text(u'备注',readonly=False,required=False,translate=False)
    FAddperson =fields.Many2one('hr.employee',u'经办人',readonly=False,required=False,translate=False)
    FAdddate= fields.Datetime(u'添加日期',readonly=False,required=False,translate=False)    
    FConsultant=fields.Many2one('hr.employee',u'班主任',readonly=False,required=False,translate=False)					   
    Fstatus=fields.Selection([('-1',u'提交'),('0',u'同意'),('1',u'驳回'),('2',u'废除'),('3',u'结束')],u'结果',readonly=False,required=False,translate=False)					   
    FDisable=fields.Selection([('0',u'启用'),('1',u'停用')],u'是否停用',readonly=False,required=False,translate=False)
    #state=fields.Selection([('draft',u'草稿'),('application',u'提报'),('approval1',u'村两委审核'),('approval2',u'村民代表审核'),('approval3',u'挂村领导审核'),('reject',u'驳回'),('approval4',u'审核通过')], u'状态', required=False, default='draft')     
fhpulic_wizardline()   